package com.jk.vehicle.mini.service.impl;

import com.alibaba.excel.util.CollectionUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.jk.vehicle.mini.dao.AdPreviewRecordMapper;
import com.jk.vehicle.mini.entity.AdPreviewRecord;
import com.jk.vehicle.mini.service.AdPreviewRecordService;
import com.jk.vehicle.mini.util.QueryUtil;
import com.jk.vehicle.mini.util.SequenceUtils;
import com.jk.vehicle.core.service.impl.AbstractService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.mapperhelper.EntityHelper;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* <p>
* 广告浏览记录 业务层实现类
* </p>
*
* @author jiajiang
* @since 2021-05-07
*/
@Service
public class AdPreviewRecordServiceImpl extends AbstractService<AdPreviewRecord> implements AdPreviewRecordService {

    /**
     * 注入AdPreviewRecordMapper
     */
    @Resource
    private AdPreviewRecordMapper adPreviewRecordMapper;

    /**
     * 根据查询条件查询单表数据
     * @param paramMap 查询条件
     * @return 查询结果
     */
    @Override
    public List<AdPreviewRecord> queryByParams(Map<String, Object> paramMap){
        Condition condition = new Condition(AdPreviewRecord.class);
        Example.Criteria criteria = condition.createCriteria();
        Map<String, EntityColumn> propertyMap = EntityHelper.getEntityTable(AdPreviewRecord.class).getPropertyMap();
        QueryUtil.spliceQueryParam(criteria, paramMap,  propertyMap, condition);
        return adPreviewRecordMapper.selectByCondition(condition);
    }

    @Override
    @DS("#last")
    public void save(AdPreviewRecord adPreviewRecord, String dsName) {
        Condition condition = new Condition(AdPreviewRecord.class);
        condition.createCriteria().andEqualTo("userId", adPreviewRecord.getUserId())
                .andEqualTo("adId", adPreviewRecord.getAdId());
        List<AdPreviewRecord> adPreviewRecordList = this.findByCondition(condition, dsName);
        if (CollectionUtils.isEmpty(adPreviewRecordList)){
            adPreviewRecord.setId(SequenceUtils.nextId());
            adPreviewRecord.setCreateDate(new Date());
            super.save(adPreviewRecord, dsName);
        }
    }

    @Override
    @DS("#last")
    public void update(AdPreviewRecord adPreviewRecord, String dsName){
        adPreviewRecord.setUpdateDate(new Date());
        super.update(adPreviewRecord, dsName);
    }

}
